package com.example.myap.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;

import com.example.myap.R;
import com.example.myap.Utils.SizeUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;

import androidx.viewpager.widget.ViewPager;



public class FindIndicator extends MagicIndicator {
    private Context mContext;
    private String[] tabNames = new String[]{"推荐", "新人"};
    private String indicatorColor = "#333333";
    private String clipColor = "#333333";
    private String textColor = "#333333";
    public FindIndicator(Context context) {
        super(context);
        this.mContext = context;
    }

    public FindIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
    }


    public void setViewPager(final ViewPager viewPager, ArrayList<String> tabs){
        ViewPagerHelper.bind(this, viewPager);
        CommonNavigator commonNavigator =new CommonNavigator(mContext);
        CommonNavigatorAdapter adpter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabs.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ScaleTransitionPagerTitleView simplePagerTitleView =new ScaleTransitionPagerTitleView(context);

                simplePagerTitleView.setText(tabs.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#888888"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#5956FB"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                CommonPagerIndicator indicator =new CommonPagerIndicator(context);
                float navigatorHeight = SizeUtils.dp2px(  7f);
                indicator.setDrawableHeight(navigatorHeight);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setDrawableWidth(SizeUtils.dp2px(  70f));
                indicator.setIndicatorDrawable(context.getDrawable(R.drawable.icon_tab_indicator));
                indicator.setYOffset(20);
                return indicator;
            }
        };
        commonNavigator.setAdapter(adpter);
        commonNavigator.setAdjustMode(false);
        setNavigator(commonNavigator);
    }
}